package com.mz.ozy.mvp.presenter;

import android.app.Activity;
import android.content.Context;

import com.google.gson.Gson;
import com.mz.ozy.base.BasePresenterImpl;
import com.mz.ozy.bean.AlipayAuthBean;
import com.mz.ozy.bean.CheckBean;
import com.mz.ozy.bean.EventBean;
import com.mz.ozy.bean.LoginBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.model.LoginModel;
import com.mz.ozy.mvp.view.LoginContract;
import com.mz.ozy.net.MyObserver;
import com.mz.ozy.utils.AppUtils;
import com.mz.ozy.utils.RxUtil;
import com.mz.ozy.utils.SPUtils;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import okhttp3.ResponseBody;

/**
 * Created by administrator on 2018/12/3.
 */
public class LoginPresenter extends BasePresenterImpl<LoginContract.View> implements LoginContract.Presenter {

    public static final int AUTH_TYPE_WX = 1;
    public static final int AUTH_TYPE_ALIPAY = 2;

    private LoginModel mModel;

    public LoginPresenter() {
        mModel = new LoginModel();
    }


    @Override
    public void onLogin(final Context context, final String phone, final int type, String password, String deviceid) {
        mModel.onLogin(context, phone, type, password, deviceid)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<LoginBean>(mView) {

                    @Override
                    public void onSuccesss(LoginBean loginBean) {
                        AppUtils.saveLoginData(context, loginBean, type);
                        //发广播通知已登录
                        EventBus.getDefault().post(new EventBean(Constants.EventCode.LOGIN_SUCCESS, null));
                        SPUtils.getInstance(context).putString(Constants.SPName.USER, phone);
                        mView.onLoginSuccess(loginBean);
                    }
                });
    }

    @Override
    public void onGetAlipayAuth(Context context) {
        mModel.onGetAlipayAuth(context)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<JSONObject>(mView) {

                    @Override
                    public void onSuccesss(JSONObject jsonObject) {
                        mView.onGetAlipayAuthSuccess(jsonObject.optString("sdkUrl"));
                    }
                });
    }

    @Override
    public void onAuthLogin(Context context, int type, String auth_code, String openid) {
        mModel.onAuthLogin(context, type, auth_code, openid)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<JSONObject>(mView) {

                    @Override
                    public void onSuccesss(JSONObject result) {
                        //登录成功Constants.TransCode.TransCode10063
                        LoginBean loginBean = new Gson().fromJson(result.toString(), LoginBean.class);
                        AppUtils.saveLoginData(context, loginBean, type);
                        EventBus.getDefault().post(new EventBean(Constants.EventCode.LOGIN_SUCCESS, null));
                        mView.onLoginSuccess(loginBean);
                    }
                }.setTransCode(Constants.TransCode.TransCode10063));
    }

    @Override
    public void checkUpdateVersion(Activity activity) {
        mModel.checkUpdateVersion(activity)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<CheckBean>(mView) {

                    @Override
                    public void onSuccesss(CheckBean jsonObject) {
                        mView.checkUpdateVersionSuccess(jsonObject);
                    }
                });
    }

}
